package server

import (
	"fmt"
	"gopkg.in/ini.v1"
	"os"
)

type GlobalConfig struct {
	DbSecret string //数据库密码
	ServerIP string //当前服务器serverip
}

func (conf *GlobalConfig) TakeValue(iniFile *ini.File) {
	conf.DbSecret = iniFile.Section("").Key("dbsecret").String()
	conf.ServerIP = iniFile.Section("").Key("server_ip").String()
}

//读取本地配置
func ReadConfig() *GlobalConfig {
	var config GlobalConfig

	cfg, err := ini.Load("config.ini")
	if err != nil {
		fmt.Printf("Fail to read config.ini: %v", err)
		os.Exit(1)
	}
	config.TakeValue(cfg)

	cfgLocal, err := ini.Load("config.ini.local")
	if err == nil {
		config.TakeValue(cfgLocal)
	}

	fmt.Println("read local config:", config)
	return &config
}
